// miniprogram/pages/member/member.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    users: [],
    page: 1,
    pageSize: 20,
    show: false,
    selectedIndex: '',
    selectedUser:{},
    showContribution:false,
    remark:'',
    point:0,
    actions: [
      {
        name: '审核通过',

      },
      {
        name: '已退营',
      },
      {
        name: '添加贡献分',
      }
    ],
    searchText:'',
  },

  onClickBtn: function (e) {
    console.log('onClickApprove', e)
    this.setData({ show: true });
    this.setData({ selectedIndex: e.currentTarget.dataset.index });
    this.setData({ selectedUser: e.currentTarget.dataset.user });
  },
  onClickUser: function (e) {
    var user = e.currentTarget.dataset.user
    wx.navigateTo({
      url: '../userDetail/detail?openid=' + user._openid + '&nickName=' + user.nickName + '&avatarUrl=' + user.avatarUrl,
    })
  },
  onSelect(event) {
    console.log(event.detail);
    switch (event.detail.name) {
      case "审核通过":
        wx.showModal({
          title: '确认',
          content: '确认通过审核吗？',
          confirmText: '确认',
          cancelText: '取消',
          success: res => {
            if (res.confirm) {
              wx.showLoading({
                title: '审核中...',
              })
              wx.cloud.callFunction({
                name: 'approveMember',
                data: {
                  id: this.data.selectedIndex,
                },
                success: res => {
                  console.log(res)
                  if (res.result.success) {
                    wx.hideLoading()
                    wx.showToast({
                      title: '审核成功',
                      icon: 'success',
                      duration: 2000
                    })
                  } else {
                    wx.showToast({
                      title: '审核失败',
                      icon: 'error',
                      duration: 2000
                    })
                  }
                },
                fail: err => {
                  wx.hideLoading()
                }
              })
            }
          }
        })
        break
        case "已退营":
          wx.showModal({
            title: '确认',
            content: '确认队员已经退出训练营吗？',
            confirmText: '确认',
            cancelText: '取消',
            success: res => {
              if (res.confirm) {
                const db = wx.cloud.database();
                const _ = db.command;
                let _openid = this.data.selectedUser._openid
                wx.showLoading({
                  title: '更新中...',
                })
                wx.cloud.callFunction({
                  name: 'knockOutMember',
                  data: {
                    _openid: _openid,
                  },
                  success: res => {
                    console.log(res)
                    if (res.result.success) {
                      console.log(res)
                      wx.hideLoading()
                      wx.showToast({
                        title: '设置退营成功！',
                        icon: 'success',
                        duration: 2000
                      })
                    } else {
                        console.log(res)
                    wx.showToast({
                      title: '设置退营失败，请联系管理员手动设置！',
                      icon: 'error',
                      duration: 2000
                    })
                    wx.hideLoading()
                    }
                  },
                  fail: err => {
                    wx.hideLoading()
                  }
                })
              }
            }
          })
          break
      case "添加贡献分":
        this.setData({
          showContribution: !this.data.showContribution,
          remark: ''
        })
        break

    }
    
  },
  onClose() {
    this.setData({ show: false });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const db = wx.cloud.database();
    db.collection('user').limit(20).get({
      success: res => {
        console.log(res.data);
        this.setData({ users: res.data })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      pagesize: 20,
    });
    const db = wx.cloud.database();
    wx.showNavigationBarLoading() //在标题栏中显示加载
    db.collection('user').limit(20).get({
      success: res => {
        console.log(res.data);
        this.setData({ users: res.data })
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.page++
    this.setData({
      page: this.data.page,
      pagesize: 20
    });
    const db = wx.cloud.database();
    let skip = (this.data.page - 1) * 20
    db.collection('user').skip(skip).limit(20).get({
      success: res => {
        this.setData({ users: this.data.users.concat(res.data) })
      }
    });
  },

  confirmM: function () {
    this.setData({
      showContribution: false,
    })
    let avatarUrl = this.data.selectedUser.avatarUrl
    let nickName = this.data.selectedUser.nickName
    let openid = this.data.selectedUser._openid
    wx.showLoading({
      title: '加载中...',
    })
    wx.cloud.callFunction({
      name: 'addContributionPoint',
      data: {
        remark: this.data.remark,
        point:this.data.point,
        openId:openid,
        avatarUrl: avatarUrl,
        nickName: nickName,
      },
      success: res => {
        console.log(res)
        wx.hideLoading()
        wx.showModal({
          content: res.result.message,
          confirmText: '我知道了',
          showCancel: false,
          success: function (res) {
          }
        })
      },
      fail: err => {
        wx.hideLoading()
        console.log(err)
      }
    })
  },

  cancelM: function () {
    this.setData({
      showContribution: false,
      remark: ''
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onClickSearch:function(){
    console.log('searchText'+this.data.searchText)
    const db = wx.cloud.database();
    wx.showNavigationBarLoading() //在标题栏中显示加载
    const {searchText} = this.data;
   const request = searchText ?  db.collection('user').where({index: this.data.searchText}): db.collection('user').limit(20);
   request.get({
      success: res => {
        console.log(res.data);
        this.setData({ users: res.data })
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    });
  }
})